package com.tan.kotlintest.activity

import android.os.Environment
import android.view.View
import com.hw.photomovie.PhotoMovieFactory
import com.hw.photomovie.PhotoMoviePlayer
import com.hw.photomovie.model.PhotoData
import com.hw.photomovie.model.PhotoSource
import com.hw.photomovie.model.SimplePhotoData
import com.hw.photomovie.render.GLTextureMovieRender
import com.tan.kotlintest.R
import com.tan.kotlintest.base.BaseActivity
import kotlinx.android.synthetic.main.activity_test_photo_movie_layout.*
import java.util.*

/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述:  测试PhotoMovie的用法
 * 作者：txj
 * 创建时间：2018/9/18
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class TestPhotoMovieActivity : BaseActivity() {

    private var mPhotoMoviePlayer: PhotoMoviePlayer? = null

    override fun getContentView(): View? {
        return inflate(R.layout.activity_test_photo_movie_layout)
    }

    override fun onContentAdded() {
        setTitleText(R.string.test_photo_movie)
        usePhotoMovie()
    }

    /**
     * 使用PhotoMovie
     */
    private fun usePhotoMovie() {
        val pathOne = Environment.getExternalStorageDirectory().absolutePath + "/image_1.png"
        val pathTwo = Environment.getExternalStorageDirectory().absolutePath + "/image_2.png"
        val pathThree = Environment.getExternalStorageDirectory().absolutePath + "/image_beauty.png"
        val pathFour = Environment.getExternalStorageDirectory().absolutePath + "/mayun.jpg"

        val photoDataList = LinkedList<PhotoData>()
        photoDataList.add(SimplePhotoData(applicationContext, pathOne, PhotoData.STATE_LOCAL))
        photoDataList.add(SimplePhotoData(applicationContext, pathTwo, PhotoData.STATE_LOCAL))
        photoDataList.add(SimplePhotoData(applicationContext, pathThree, PhotoData.STATE_LOCAL))
        photoDataList.add(SimplePhotoData(applicationContext, pathFour, PhotoData.STATE_LOCAL))

        val photoSource = PhotoSource(photoDataList)
        //generate PhotoMoive
        val photoMovie = PhotoMovieFactory.generatePhotoMovie(photoSource, PhotoMovieFactory.PhotoMovieType.GRADIENT)
        //generate MovieRenderer for rendering PhotoMovie to glTextureView
        val movieRenderer = GLTextureMovieRender(gl_texture)
        /**
         * OR  MovieRenderer movieRenderer = new GLSurfaceMovieRenderer(glSurfaceView);
         */
        //PhotoMoviePlayer
        mPhotoMoviePlayer = PhotoMoviePlayer(applicationContext)
        mPhotoMoviePlayer?.setMovieRenderer(movieRenderer)
        //photoMoviePlayer.setMovieListener()
        mPhotoMoviePlayer?.setLoop(true)
        mPhotoMoviePlayer?.setOnPreparedListener(object : PhotoMoviePlayer.OnPreparedListener {
            override fun onPrepared(p0: PhotoMoviePlayer?, p1: Int, p2: Int) {
                runOnUiThread { mPhotoMoviePlayer?.start() }
            }

            override fun onPreparing(p0: PhotoMoviePlayer?, p1: Float) {
            }

            override fun onError(p0: PhotoMoviePlayer?) {
            }

        })
        mPhotoMoviePlayer?.setDataSource(photoMovie)
        mPhotoMoviePlayer?.prepare()
    }

    override fun onPause() {
        super.onPause()
        mPhotoMoviePlayer?.destroy()
        gl_texture.onPause()
    }


}